<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>login</title>
</head>

<body>
  <div>
    <h1>登录界面</h1>
    <div>
      用户名：<input type="text" id="username">
    </div>
    <div>
      密码：<input type="password" id="password">
    </div>
    <div>
      <button id="login">登录</button>
    </div>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script>
    axios.interceptors.request.use(function (config) {
      return config;
    }, function (error) {
      return Promise.reject(error);
    });
    axios.interceptors.response.use(function (response) {
      const token = response.headers.authorization;
      // console.log(token);
      token && localStorage.setItem('token', token);
      return response;
    }, function (error) {
      return Promise.reject(error);
    });
  </script>
  <script>
    var login = document.querySelector('#login');
    var username = document.querySelector('#username');
    var password = document.querySelector('#password');

    login.onclick = () => {
      axios.post('/api/login', {
        username: username.value,
        password: password.value,
      }).then(res => {
        console.log(res)
        if (res.data.status === 'success' && res.data.data) {
          window.location.href = '/'
        } else {
          alert('账号密码不匹配')
        }
      })
    }
  </script>
</body>

</html>